package com.sinodata.bsm.cicp.collector.sybase;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * Process.java
 * <p>
 * 
 * @author 管普贵 guanpg@uni-mon.com
 * @version 2.0
 * @Date Jan 8, 2010
 */
public class Process extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(Process.class);

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            String serverName = this.getParamMap().get("ServerName");
            String sql = "SELECT status, suid, time_blocked FROM master.dbo.sysprocesses";
            String[] rs = this.getStrings(serverName, sql, ip);

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sybase process", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql, String ip) throws CollectorException {
        String[] propValArr = new String[5];
        try {
            ResultSet rst = this.getCachedResultSet("sybase", ip, serverName, sql);
            int procCount = 0;
            int procDeadLocks = 0;
            int actProcCount = 0;
            int sysProcCount = 0;
            int sessCount = 0;
            while (rst.next()) {
                String status = rst.getString("status");
                int suid = rst.getInt("suid");
                int time_blocked = rst.getInt("time_blocked");
                status = status == null ? "" : status.trim();

                procCount++;
                if (time_blocked > 60)
                    procDeadLocks++;
                if (status.equals("running"))
                    actProcCount++;
                if (suid == 0)
                    sysProcCount++;
                else
                    sessCount++;
            }
            propValArr[0] = procCount + "";
            propValArr[1] = procDeadLocks + "";
            propValArr[2] = actProcCount + "";
            propValArr[3] = sysProcCount + "";
            propValArr[4] = sessCount + "";

        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        }
        return propValArr;
    }

    public String[] getParamMeta() {
        return new String[] { "ServerName" };
    }

    public String[] getPropMeta() {
        //        return new String[] { "进程数", "进程死锁数", "活动进程数", "系统进程数", "当前会话数" };
        return new String[] { "Processes", "ProcessDeadlockNum", "ActiveProcesses", "SystemProcesses", "CurrentSessions" };
    }
}
